/*
 *  Copyright 2021 DadaWang
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package tech.cutu.project.syt.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.cutu.project.syt.common.result.Result;
import tech.cutu.project.syt.common.utils.MD5;
import tech.cutu.project.syt.hosp.service.HospitalSetService;
import tech.cutu.project.syt.model.hosp.HospitalSet;
import tech.cutu.project.syt.vo.hosp.HospitalSetQueryVo;

import java.util.List;
import java.util.Random;

/**
 * <a href="http://localhost:8201/swagger-ui.html">医院设置管理</a>
 *
 * @author DadaWang
 * @version 1.0.0
 * @since 2021-05-06
 */
@Api(value = "医院设置管理", tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询所有医院设置", tags = "医院设置管理")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAllHospitalSet(){
        return Result.ok(this.hospitalSetService.list());
    }

    @ApiOperation(value = "删除医院设置", tags = "医院设置管理")
    @DeleteMapping("{id}")
    public Result<Boolean> removeHospitalSet(@PathVariable("id") Long id){
        boolean deletedFlag = this.hospitalSetService.removeById(id);
        return deletedFlag ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "分页查询医院设置", tags = "医院设置管理")
    @PostMapping("/findPage/{current}/{limit}")
    public Result<Page<HospitalSet>> findPage(@PathVariable("current") Integer current, @PathVariable("limit") Integer limit, @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        // 创建Page对象
        Page<HospitalSet> page = new Page<>(current, limit);
        LambdaQueryWrapper<HospitalSet> wrapper = new LambdaQueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like(HospitalSet::getHosname, hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq(HospitalSet::getHoscode, hoscode);
        }
        this.hospitalSetService.page(page, wrapper);
        return Result.ok(page);
    }

    @ApiOperation(value = "新增医院设置", tags = "医院设置管理")
    @PostMapping("/save")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean savedFlag = this.hospitalSetService.save(hospitalSet);
        return savedFlag ? Result.ok() : Result.fail();
    }

}
